package com.example.administrator.guosai11gonggoncesuo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.nle.mylibrary.enums.led.PlayType;
import com.nle.mylibrary.enums.led.ShowSpeed;
import com.nle.mylibrary.forUse.led.LedScreen;
import com.nle.mylibrary.forUse.mdbus4150.MdBus4150SensorListener;
import com.nle.mylibrary.forUse.mdbus4150.Modbus4150;
import com.nle.mylibrary.forUse.zigbee.Zigbee;
import com.nle.mylibrary.transfer.DataBusFactory;

public class MainActivity extends AppCompatActivity {
    private TextView tv_smoke;
    private Modbus4150 modbus4150;
    private LedScreen ledScreen;
    private Zigbee zigbee;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        modbus4150=new Modbus4150(DataBusFactory.newSocketDataBus("172.18.14.201",4196));
        ledScreen=new LedScreen(DataBusFactory.newSocketDataBus("172.18.14.203",4196));
        zigbee=new Zigbee(DataBusFactory.newSocketDataBus("172.18.14.200",4196));
        tv_smoke=findViewById(R.id.tv_smoke);
        try {
            ledScreen.switchLed(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Mythread mythread =new Mythread();
        mythread.start();
    }
    private class  Mythread extends Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();

                }
                try {
                    modbus4150.getVal(5, new MdBus4150SensorListener() {
                        @Override
                        public void onVal(int val) {
                            if (val==0){
                                tv_smoke.setText("烟雾状态：没烟");
                            }else if(val==1){
                                tv_smoke.setText("烟雾状态：有烟");
                            }

                        }
                    });
                    double person = zigbee.getPerson();
                    if(person==1){
                        try {
                            ledScreen.sendTxt("有人",PlayType.LEFT,ShowSpeed.SPEED1,1,100);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    else if(person==0){
                        try {
                            ledScreen.sendTxt("没人",PlayType.LEFT,ShowSpeed.SPEED1,1,100);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    /*modbus4150.getVal(6, new MdBus4150SensorListener() {
                        @Override
                        public void onVal(int val) {
                            if (val==0){
                                try {
                                    ledScreen.sendTxt("没人",PlayType.LEFT,ShowSpeed.SPEED1,1,100);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }else if(val==1){
                                try {
                                    ledScreen.sendTxt("有人",PlayType.LEFT,ShowSpeed.SPEED1,1,100);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        }
                    });*/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
